<template>
  <div>
    <h2>Book</h2>
    <!--表格-->
    <el-table :data="page.records" stripe border>
      <el-table-column prop="id" label="ID" show-overflow-tooltip/>
      <el-table-column prop="name" label="Name" show-overflow-tooltip/>
      <el-table-column prop="price" label="Price" show-overflow-tooltip/>
    </el-table>
    <!--分页-->
    <el-pagination
      v-model:current-page="page.current"
      page-size="10"
      :size="page.size"
      :total="page.total"
      :page-count="page.pages"
      layout="total, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { useAuthStore } from "@/stores/auth"
import axios from "axios";
import {onMounted, ref} from "vue";
import type { Book, Page } from "@/types";

const authStore = useAuthStore()

const page = ref<Page<Book>>({
  records: [],
  total: 0,
  size: 10,
  current: 1,
  pages: 0
})

function fetchBook(current: number, size: number) {
  axios({
    method: "get",
    url: "http://192.168.101.15:18080/api/book",
    headers: {
      Authorization: `Bearer ${authStore.getJwt()}`
    },
    params: {
      page: current,
      size: size
    }
  }).then(response => {
    page.value = response.data
  });
}

onMounted(() => {
  fetchBook(page.value.current, page.value.size)
})

function handleSizeChange(size: number) {
  fetchBook(page.value.current, size)
}

function handleCurrentChange(current: number) {
  fetchBook(current, page.value.size)
}
</script>

<style scoped>

</style>